package com.EJ.ejcompanyweb.updata;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;

import com.EJ.ejcompanyweb.R;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import constacne.UiType;
import listener.UpdateDownloadListener;
import model.UiConfig;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class UpdataAsyncTask extends AsyncTask<String,Void,UpdataMessage> {
  private   Context context;
    private String TAG=this.getClass().getSimpleName();
    private  UpdataMessage message;

    public UpdataAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected UpdataMessage doInBackground(String... params) {
        try{
            //1.将url网络地址字符串封装成url对象
            URL url=new URL(params[0]);
            //2.打开url对象的网络链接  获取网络链接对象
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
            //3.进行网络链接的设置
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            //4.获取响应状态码
            /**
             200(正常),206请求部分数据
             302/307(临时重定向),
             304(未修改),
             404(找不到),
             500(服务器内部错误)
             */
            int responseCode=conn.getResponseCode();
            //5.判断状态码  如果为200说明成功则进行读取操作
            if(responseCode==200){
                //开始读取网络输入流
                InputStream inputStream=conn.getInputStream();
                message =new PullParserHelper().startParser(inputStream);
            }
            conn.disconnect();//关掉连接
        }catch (Exception e){
            e.printStackTrace();
        }
        //返回字节数组
        return message;
    }
    @Override
    protected void onPostExecute(UpdataMessage updataMessage) {
        super.onPostExecute(updataMessage);
        if (updataMessage==null){
            return;
        }
            Log.e(TAG,updataMessage.toString());
        if (updataMessage!=null&&updataMessage.getServerVersionCode()>getVersionCode()){
            UpdateConfig   updateConfig = new UpdateConfig();
            updateConfig.setCheckWifi(true);
            updateConfig.setNeedCheckMd5(false);
            updateConfig.setShowNotification(true);
            updateConfig.setNotifyImgRes(R.drawable.ic_update_logo);
            //服务器上apk版本名
            updateConfig.setServerVersionName(updataMessage.getServerVersionName());
            //是否强制更新，强制时无取消按钮
            updateConfig.setForce(true);

            // updateConfig.setApkSavePath(Environment.getExternalStorageDirectory().getAbsolutePath() +"/teprinciple");
            // Log.e(TAG,updateConfig.getApkSavePath());
            UiConfig uiConfig = new UiConfig();
            uiConfig.setUiType(UiType.PLENTIFUL);
            UpdateAppUtils
                    .getInstance()
                    .apkUrl(updataMessage.getApkUrl())
                    .updateTitle(updataMessage.getUpdateTitle())
                    .updateContent(updataMessage.getUpdateContent())
                    .uiConfig(uiConfig)
                    .updateConfig(updateConfig)
                    .setUpdateDownloadListener(new UpdateDownloadListener() {
                        @Override
                        public void onStart() {
                        }
                        @Override
                        public void onDownload(int i) {
                            Log.e(TAG,"下载进度"+i);
                        }
                        @Override
                        public void onFinish() {
                        }
                        @Override
                        public void onError(Throwable throwable) {
                        }
                    })
                    .update();
        }

    }
    private  int getVersionCode(){
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(),0);
            Log.e(TAG,pi.versionCode+"");
            return pi.versionCode;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
